package com.tax.config.filter;

import com.tax.config.exception.ApiException;
import com.tax.config.response.Response;
import com.tax.config.response.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 统一异常处理
 */
@Slf4j
@ControllerAdvice(annotations = {RestController.class})
public class GlobalExceptionHandler {

    /**
     * 处理未知异常
     * @param exception
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Response bizExceptionHandle(Exception exception) {
        log.error("GlobalExceptionHandler>>bizExceptionHandle,{}", ExceptionUtils.getStackTrace(exception));
        return new Response<>(ResponseCode.SYSTEM_ERROR.getCode(),ResponseCode.SYSTEM_ERROR.getMsg());
    }

    @ExceptionHandler(value = ApiException.class)
    @ResponseBody
    public Response handlerBizException( ApiException bizException) {
        log.error("GlobalExceptionHandler>>BizException,{}", ExceptionUtils.getStackTrace(bizException));
        String key = bizException.getKey();
        String message = bizException.getMessage();
        return new Response<>(Integer.valueOf(key), message);
    }

    /**
     * 参数验证错误异常处理
     * @param methodArgumentNotValidException
     * @return
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public Response bizMethodArgsNotValidExceptionHandle( MethodArgumentNotValidException methodArgumentNotValidException) {
        log.error("GlobalExceptionHandler>>MethodArgumentNotValidException,{}",ExceptionUtils.getStackTrace(methodArgumentNotValidException));
        BindingResult bindingResult = methodArgumentNotValidException.getBindingResult();
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        Map<String,String> errMap=new HashMap<>();
        final String[] errMsg = {"参数为空"};
        fieldErrors.stream().forEach(fieldfError->{
            String field = fieldfError.getField();
            String defaultMessage = fieldfError.getDefaultMessage();
            errMap.put(field,defaultMessage);
            errMsg[0] = defaultMessage;
        });
        return new Response<>(ResponseCode.SYSTEM_ERROR.getCode(), errMsg[0]);
    }

    /**
     * 参数验证错误异常处理
     * @param bindException
     * @return
     */
    @ExceptionHandler(value =BindException.class)
    @ResponseBody
    public Response bizBindExceptionHandle(BindException bindException) {
        log.error("GlobalExceptionHandler>>bizBindExceptionHandle,{}",ExceptionUtils.getStackTrace(bindException));
        BindingResult bindingResult = bindException.getBindingResult();
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        Map<String,String> errMap=new HashMap<>();
        final String[] errMsg = {"参数为空"};
        fieldErrors.stream().forEach(fieldfError->{
            String field = fieldfError.getField();
            String defaultMessage = fieldfError.getDefaultMessage();
            errMap.put(field,defaultMessage);
            errMsg[0] = defaultMessage;
        });
        return new Response<>(ResponseCode.PARA_ERROR.getCode(), errMsg[0]);
    }


}
